package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.TPlatformTypeMapper;
import com.hbnu.system.model.entity.TPlatformType;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.TPlatformTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TPlatformTypeImpl extends ServiceImpl<TPlatformTypeMapper, TPlatformType> implements TPlatformTypeService {

    @Override
    public List<NameId> getCascadingData() {
        List<TPlatformType> tPlatformTypeList = baseMapper.selectList(Wrappers.lambdaQuery(TPlatformType.class));

        // 1. 一级类型: Type1ID -> 名称
        Map<Integer, String> type1Map = new HashMap<>();
        // 2. 二级类型: 使用(一级Type1ID, 二级Type2ID)组合作为唯一键
        Map<String, String> type2Map = new HashMap<>();
        Map<String, Integer> type2ToType1 = new HashMap<>();
        // 3. 三级类型: 用记录主键ID作为唯一标识，关联(一级Type1ID, 二级Type2ID)组合
        Map<Integer, String> type3Map = new HashMap<>();
        Map<Integer, String> type3ToType2Key = new HashMap<>(); // 记录ID -> (一级Type1ID, 二级Type2ID)组合

        // 收集所有类型信息
        for (TPlatformType pt : tPlatformTypeList) {
            // 收集一级类型
            if (pt.getType1Id() != 0 && pt.getProjectType1() != null && !pt.getProjectType1().isEmpty()) {
                type1Map.put(pt.getType1Id(), pt.getProjectType1());
            }

            // 收集二级类型，使用(一级Type1ID, 二级Type2ID)组合作为唯一键
            if (pt.getType2Id() != 0 && pt.getType2() != null && !pt.getType2().isEmpty()) {
                String type2Key = pt.getType1Id() + "_" + pt.getType2Id(); // 组合键
                type2Map.put(type2Key, pt.getType2());
                type2ToType1.put(type2Key, pt.getType1Id());
            }

            // 收集三级类型
            if (pt.getType3Id() != 0 && pt.getType3() != null && !pt.getType3().isEmpty()) {
                int recordId = pt.getId();
                type3Map.put(recordId, pt.getType3());
                String type2Key = pt.getType1Id() + "_" + pt.getType2Id();
                type3ToType2Key.put(recordId, type2Key);
            }
        }

        // 构建三级 -> 二级的子节点映射
        Map<String, List<NameId>> type2Children = new HashMap<>();
        for (Map.Entry<Integer, String> type3Entry : type3Map.entrySet()) {
            Integer recordId = type3Entry.getKey();
            String type3Name = type3Entry.getValue();
            String parentType2Key = type3ToType2Key.get(recordId); // 获取(一级Type1ID, 二级Type2ID)组合

            // 将三级节点添加到对应二级节点的子列表
            type2Children.computeIfAbsent(parentType2Key, k -> new ArrayList<>())
                    .add(new NameId(type3Name, recordId));
        }

        // 构建二级 -> 一级的子节点映射
        Map<Integer, List<NameId>> type1Children = new HashMap<>();
        for (Map.Entry<String, String> type2Entry : type2Map.entrySet()) {
            String type2Key = type2Entry.getKey(); // (一级Type1ID, 二级Type2ID)组合
            String type2Name = type2Entry.getValue();

            // 从组合键中解析出一级Type1ID和二级Type2ID
            String[] parts = type2Key.split("_");
            Integer parentType1Id = Integer.parseInt(parts[0]);
            Integer type2Id = Integer.parseInt(parts[1]); // 使用原始的Type2ID作为value

            // 获取当前二级节点的三级子节点
            List<NameId> thirdLevel = type2Children.getOrDefault(type2Key, new ArrayList<>());

            // 构建二级节点(有子节点则为ChildrenNameId，否则为NameId)
            if (thirdLevel.isEmpty()) {
                type1Children.computeIfAbsent(parentType1Id, k -> new ArrayList<>())
                        .add(new NameId(type2Name, type2Id));
            } else {
                ChildrenNameId secondLevel = new ChildrenNameId(type2Name, type2Id);
                secondLevel.setChildren(thirdLevel);
                type1Children.computeIfAbsent(parentType1Id, k -> new ArrayList<>())
                        .add(secondLevel);
            }
        }

        // 构建最终的一级节点列表
        List<NameId> result = new ArrayList<>();
        for (Map.Entry<Integer, String> type1Entry : type1Map.entrySet()) {
            Integer type1Id = type1Entry.getKey();
            String type1Name = type1Entry.getValue();

            // 获取当前一级节点的二级子节点
            List<NameId> secondLevel = type1Children.getOrDefault(type1Id, new ArrayList<>());

            // 构建一级节点(有子节点则为ChildrenNameId，否则为NameId)
            if (secondLevel.isEmpty()) {
                result.add(new NameId(type1Name, type1Id));
            } else {
                ChildrenNameId firstLevel = new ChildrenNameId(type1Name, type1Id);
                firstLevel.setChildren(secondLevel);
                result.add(firstLevel);
            }
        }

        return result;
    }




}